package com.huxiaohui.ssbx.agent.appoperation.tools;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.huxiaohui.ssbx.constants.ResponseConstants;
import com.huxiaohui.ssbx.utils.ResponseFutureHolder;
import com.huxiaohui.ssbx.utils.SessionHolder;
import com.huxiaohui.ssbx.utils.SessionMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@Slf4j
public class AppOperationTools {

    @Tool(description = "获取可以操作的应用程序和该应用程序所支持的功能")
    public String getSupportOpertionsApp(@ToolParam (description = "会话ID") String sessionId ) throws ExecutionException, InterruptedException {
        String requestId = IdUtil.fastSimpleUUID();
        Map<String, Object> result = new HashMap<>();
        result.put("requestId", requestId);
        result.put("get",true);

        // 创建CompletableFuture用于阻塞和接收结果
        CompletableFuture<String> future = new CompletableFuture<>();
        ResponseFutureHolder.put(requestId, future);
        try {
            // 通过session发送信息给客户端
            SessionMessage.sendMessage(SessionHolder.get(sessionId), JSONUtil.toJsonStr(result), ResponseConstants.APP_OPERATION);
            return future.get();
        } catch (Exception e) {
            log.debug("获取可以操作的应用程序和该应用程序所支持的功能失败", e);
        } finally {
            ResponseFutureHolder.remove(requestId);
        }
        return "处理失败";
    }
}